package com.zbf.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

/**
 * @author: LI-CI-GAO
 * 作者: licigao
 * 日期: 2021/9/9  9:22
 * 描述:
 */
@Component
public class TestService {

    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;

    @Autowired
    private TransactionDefinition transactionDefinition;


    public void insertObject(){

        TransactionStatus transaction = dataSourceTransactionManager.getTransaction(transactionDefinition);

        try{

            //提交事务
            dataSourceTransactionManager.commit(transaction);
        }catch (Exception e){
            dataSourceTransactionManager.rollback(transaction);
        }

    }

}
